import { Component, OnInit, Injector, TemplateRef, ViewChild, Input } from '@angular/core';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { HttpService } from "../../core/service/http.service";
import { HttpClient } from "@angular/common/http";
import { Router, NavigationExtras } from "@angular/router";
import {
  AndOr, Compare, ExpressionPair, PagedResultDto, PagerSearchInput, ElementViewType, BaseComponentConfig,
  SystemFormControl, FieldType, ElementShowType, CustomNavigation, ClassInfoOutput, NavigatType, ArSystemFormControl, ClassSchool, TeacherOutput
} from "../..";
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { CityListComponent } from '../school/cityList';
import {
  FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators,
  FormControl
} from "@angular/forms";
import { LoadRemoteDataType } from '../../core/model/CommonDto';



@Component({
  selector: 'app-teachermanage',
  templateUrl: './teachermanage.component.html',
  styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class TeachermanageComponent extends NgZorroPageBaseComponent implements OnInit {
  // 添加课程和班级的列表
  _listClassOrSubject = [];
  _addClassOrSubject = 0;
  // 重置密码
  repassword = '';
  isVisiblemm = false;
  reid = 0;
  // 获取班级或者subject信息
  listClassOrSubjectLoading = false;


  ngOnInit() {
    this.IniObserver.subscribe(sb => {
      this.getEntityList();
    });
  }
  constructor(
    injecter: Injector
  ) {
    super(injecter,
      new BaseComponentConfig("TeacherInfo/TeacherInfoPager", "TeacherInfo/Create", "TeacherInfo/Update",
        "TeacherInfo/Get", "TeacherInfo/Delete", "id", "", "教师信息管理"
      ),
      new ArSystemFormControl(new Array<SystemFormControl>(
        new SystemFormControl("departmentId", "院系选择", FieldType.Select, true, 30, 0, "", 'School/GetDepartmentByAcademe', false, null, LoadRemoteDataType.LoadAll, true, false, false),
        new SystemFormControl("userName", "用户名", FieldType.Text, true, 30, 0, "", "", false, null, null),
        new SystemFormControl("password", "密码", FieldType.Text, true, 30, 0, "", "", false, null, null, true, false),
        new SystemFormControl("personName", "老师姓名", FieldType.Text, true, 30, 0, "", "", false, null, null),
        new SystemFormControl("phoneNumber", "老师电话", FieldType.Text, false, 30, 0, "", "", false, null, null),
        new SystemFormControl("drillProjectIdList", "负责课程", FieldType.Text, false, 300, 0, "", "", false, null, null, false, false),
        new SystemFormControl("classinfoIdList", "关联班级", FieldType.Text, false, 300, 0, "", "", false, null, null, false, false),
        new SystemFormControl("projectNames", "负责课程", FieldType.Text, false, 300, 0, "", "", false, null, null, false),
        new SystemFormControl("classNames", "关联班级", FieldType.Text, false, 300, 0, "", "", false, null, null, false)
      ))
    );
  }

  async getClassOrSubject(e: MouseEvent, isSubject) {
    e.preventDefault();
    this.listClassOrSubjectLoading = true;
    let url = '';
    if (isSubject) {
      url = `teacherinfo/GetDrillProjectByAcade?aid=0&teacherid=${this.focusEntity.id}`;
      this._listClassOrSubject = await this.httpService.get<any>(url);
      this._listClassOrSubject.forEach(item => {
        if (this.focusEntity.drillProjectIdList && this.focusEntity.drillProjectIdList.filter(find => find === item.key).length > 0) {
          item.isChecked = true;
        }
      });
      this._addClassOrSubject = 1;
    } else {
      this._listClassOrSubject = await this.httpService.post<any>('teacherinfo/GetClassInfo', { teacherId: this.focusEntity.id, projectIdList: this.focusEntity.drillProjectIdList });
      this._listClassOrSubject.forEach(item => {
        if (this.focusEntity.classinfoIdList && this.focusEntity.classinfoIdList.filter(find => find === item.key).length > 0) {
          item.isChecked = true;
        }
      });
      this._addClassOrSubject = 2;
    }
    this.listClassOrSubjectLoading = false;
  }

  async saveClassOrSubject() {
    if (this._addClassOrSubject === 1) {
      this.focusEntity.drillProjectIdList.splice(0, this.focusEntity.drillProjectIdList.length);
    } else {
      this.focusEntity.classinfoIdList.splice(0, this.focusEntity.classinfoIdList.length);
    }
    this._listClassOrSubject.forEach(e => {
      if (e.isChecked) {
        if (this._addClassOrSubject === 1) {
          this.focusEntity.drillProjectIdList.push(e.key);
        } else {
          this.focusEntity.classinfoIdList.push(e.key);
        }
      }
    });
    this._addClassOrSubject = 0;
  }

  getNewEntity(): TeacherOutput {
    return new TeacherOutput();
  }

  async navigateCustom(url: string, pid: any) {

  }

  showModalForComponent(entity: any, tag: any) {
    throw new Error("Method not implemented.");
  }


  async ResetPasswords(id) {
    this.reid = id;
    const repassword1 = await this.httpService.get<any>('SchoolAcademeManager/GetPwd?input=' + id);
    this.repassword = repassword1;
    this.isVisiblemm = true;
  }

  async handleOkmm(type: string) {
    if (this.repassword === "") {
      this.message.info('密码不能为空');
    } else {
      const data = { "id": this.reid, "userPwd": this.repassword };
      try {
        this.httpService.post<any>('SchoolAcademeManager/UpdatePassword', data);
      } finally {

      }

      this.isVisiblemm = false;
      this.message.create(type, `密码修改成功`);
    }
  }
}
